package com.xindacloud.sms.adapter.service.business.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.adapter.dao.SmsUserAllowOperatorMapper;
import com.xindacloud.sms.adapter.entity.SmsUserAllowOperator;
import com.xindacloud.sms.adapter.exception.ServiceException;
import com.xindacloud.sms.adapter.service.business.ISmsUserAllowOperatorService;
import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;


@Slf4j
@Service
public class SmsUserAllowOperatorServiceImpl extends ServiceImpl<SmsUserAllowOperatorMapper, SmsUserAllowOperator> implements ISmsUserAllowOperatorService {


    @Override
    public List<SmsUserAllowOperator> queryUserAllowOperatorById(Integer userId) {
        return lambdaQuery()
                .eq(SmsUserAllowOperator::getUserId, userId)
                .list();
    }


    @Override
    public SmsUserAllowOperator queryOneMaxPriceOrderByDesc(Integer userId, String countryCode, String prefix, String networkCode) {

        final List<SmsUserAllowOperator> operatorList = lambdaQuery()
                .eq(SmsUserAllowOperator::getUserId, userId)
                //.eq(StringUtils.isNotEmpty(networkCode), SmsUserAllowOperator::getNetworkCode, networkCode)
                .eq(SmsUserAllowOperator::getPrefix, prefix)
                .eq(SmsUserAllowOperator::getCountryCode, countryCode)
                .orderByDesc(SmsUserAllowOperator::getPrice)
                .list();

        for (SmsUserAllowOperator userAllowOperator : operatorList) {
            final String ntwCode = userAllowOperator.getNetworkCode();
            if ((StringUtils.isNotEmpty(ntwCode) && Objects.equals(networkCode, ntwCode))
                    || filterNetworkAliases(userAllowOperator.getNetworkAliases(), networkCode)) {
                return userAllowOperator;
            }

        }

        return CollectionUtils.isNotEmpty(operatorList) ? operatorList.get(0) : new SmsUserAllowOperator();
    }

    private boolean filterNetworkAliases(String networkAliases, String networkCode) {
        if (StringUtils.isNotEmpty(networkAliases)) {
            return Arrays.asList(networkAliases.split(",")).contains(networkCode);
        }
        return false;
    }

    @Override
    public List<SmsUserAllowOperator> queryMinCostPriceOrderByAsc(Integer userId, String countryCode, String prefix) {

        List<SmsUserAllowOperator> minPricingList = lambdaQuery()
                .eq(SmsUserAllowOperator::getUserId, userId)
                .eq(SmsUserAllowOperator::getStatus, true)
                .eq(SmsUserAllowOperator::getCountryCode, countryCode)
                .eq(SmsUserAllowOperator::getPrefix, prefix)
                .orderByAsc(SmsUserAllowOperator::getCostPrice)
                .last("LIMIT 500")
                .list();

        checkPriceList(countryCode, prefix, minPricingList);
        return minPricingList;
    }


    @Override
    public SmsUserAllowOperator queryOneMaxPriceByAgentId(Integer userId, String countryCode, String prefix, String agentId) {
        final List<SmsUserAllowOperator> list = lambdaQuery()
                .eq(SmsUserAllowOperator::getUserId, userId)
                .eq(SmsUserAllowOperator::getCountryCode, countryCode)
                .eq(SmsUserAllowOperator::getPrefix, prefix)
                .eq(SmsUserAllowOperator::getAgentId, agentId)
                .orderByDesc(SmsUserAllowOperator::getPrice)
                .last("LIMIT 500")
                .list();


        checkPriceList(countryCode, prefix, list);
        return list.get(0);
    }

    private void checkPriceList(String countryCode, String prefix, List<SmsUserAllowOperator> list) {
        Optional.of(list)
                .filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ServiceException(String.format("该用户未同步当前国家编码为:%s,国家前缀为:%s运营商价格", countryCode, prefix),
                        UnionResultHelp.StatusEnum.ParamsFail.code()));
    }

    @Override
    public SmsUserAllowOperator queryUserAllowOperatorMinPrice(Integer userId, String countryCode, String prefix) {

        List<SmsUserAllowOperator> minPricingList = lambdaQuery()
                .eq(SmsUserAllowOperator::getStatus, true)
                .eq(SmsUserAllowOperator::getUserId, userId)
                .eq(SmsUserAllowOperator::getCountryCode, countryCode)
                .eq(SmsUserAllowOperator::getPrefix, prefix)
                .orderByAsc(SmsUserAllowOperator::getPrice)
                .last("LIMIT 500")
                .list();

        checkPriceList(countryCode, prefix, minPricingList);
        return minPricingList.get(0);
    }
}
